/**
 * <b>项目名称：</b>网络应用框架<br/>
 * <b>包    名：</b>com.hhwy.framework.code.generation.swing<br/>
 * <b>文 件 名：</b>Config.java<br/>
 * <b>版本信息：</b><br/>
 * <b>日    期：</b>2015年4月1日-下午3:51:31<br/>
 * <b>Copyright (c)</b> 2015恒华伟业科技股份有限公司-版权所有<br/>
 * 
 */
package com.hhwy.framework.code.generation.swing;

import javax.swing.*;

import com.hhwy.framework.code.generation.CodeGenerationMain;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hhwy.framework.code.generation.util.PomUtil;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * <b>类 名 称：</b>Config<br/>
 * <b>类 描 述：</b><br/>
 * <b>创 建 人：</b>zhangbaojian<br/>
 * <b>修 改 人：</b>zhangbaojian<br/>
 * <b>修改时间：</b>2015年4月1日 下午3:51:31<br/>
 * <b>修改备注：</b><br/>
 * 
 * @version 1.0.0<br/>
 */
public class Config extends JFrame implements ActionListener {
	
	public static final Logger log = LoggerFactory
			.getLogger(Config.class);
	private static final long serialVersionUID = -4345451769150967092L;
	
	private GridBagLayout g = new GridBagLayout();
	private GridBagConstraints c = new GridBagConstraints();
	private JTextField tableName;
	private JTextField sceneName;
	private JTextField packagePath;
	private JButton submit;
	
	public Config(String str){
		super(str);
		setSize(500, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setLayout(g);
		// 调用方法
		addComponent();
		submit.addActionListener(this);
		setVisible(true);
		setLocationRelativeTo(null);// 设居中显示;
	}
	// 在这个方法中将会添加所有的组件;使用的网格包布局
	public void addComponent(){
		c.insets = new Insets(7, 0, 4, 0);
		this.addTableName();
		this.addPackagePath();
		this.addSceneName();
		
		
		// submit按钮
		c.insets = new Insets(17, 10, 14, 10);
		submit = new JButton("生成代码");
		submit.setFont(new Font (Font.DIALOG, Font.BOLD, 18));
		add(g, c, submit, 1, 5, 1, 1);
	}
	
	public void add(GridBagLayout g, GridBagConstraints c, JComponent jc,
			int x, int y, int gw, int gh){
		c.gridx = x;
		c.gridy = y;
		c.anchor = GridBagConstraints.WEST;
		c.gridwidth = gw;
		c.gridheight = gh;
		g.setConstraints(jc, c);
		add(jc);
	}

	public void actionPerformed(ActionEvent event){
		try {
//			String tableNameVlaue = tableName.getText();
//			String sceneNameVlaue = sceneName.getText();
//			String packagePathVlaue = packagePath.getText();
			String tableNameVlaue = "ac_user";
			String sceneNameVlaue = "user";
			String packagePathVlaue = "com.hhwy.epc";
			String userDir = System.getProperty("user.dir");
			if(StringUtils.isEmpty(tableNameVlaue)){
				JOptionPane.showMessageDialog(null, "表名必填", "提示", JOptionPane.INFORMATION_MESSAGE);
				return ;
			}
			if(StringUtils.isEmpty(sceneNameVlaue)){
				JOptionPane.showMessageDialog(null, "场景名必填", "提示", JOptionPane.INFORMATION_MESSAGE);
				return ;
			}
			if(StringUtils.isEmpty(packagePathVlaue)){
				JOptionPane.showMessageDialog(null, "包名必填", "提示", JOptionPane.INFORMATION_MESSAGE);
				return ;
			} else if(packagePathVlaue.indexOf(PomUtil.getProjectPackage()) == -1 || packagePathVlaue.indexOf(PomUtil.getProjectPackage()) > 0){
				JOptionPane.showMessageDialog(null, "包名必须以"+PomUtil.getProjectPackage()+"开头，规则是pom.xml中groupId.artifactId,一般为恒华伟业.插件名", "提示", JOptionPane.INFORMATION_MESSAGE);
				return ;
			}
			CodeGenerationMain.loopCreateCode(userDir, packagePathVlaue, sceneNameVlaue, tableNameVlaue);
			
			JOptionPane.showMessageDialog(null, "代码生成完成", "提示", JOptionPane.INFORMATION_MESSAGE);
		} catch (Exception e) {
			log.error("参数错误!", e);
			JOptionPane.showMessageDialog(null, "参数错误,请输入正确的参数", "错误", JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
	public void addTableName() {
		JLabel tableNameLabel = new JLabel("表名：");
		tableNameLabel.setFont(new Font (Font.DIALOG, Font.BOLD, 18));
		add(g, c, tableNameLabel, 0, 3, 1, 1);
		this.tableName = new JTextField(28);
		this.tableName.setFont(new Font (Font.DIALOG, Font.PLAIN, 18));
		add(g, c, tableName, 1, 3, 2, 1);
	}
	public void addSceneName() {
		JLabel tableNameLabel = new JLabel("场景名：");
		tableNameLabel.setFont(new Font (Font.DIALOG, Font.BOLD, 18));
		add(g, c, tableNameLabel, 0, 2, 1, 1);
		this.sceneName = new JTextField(28);
		this.sceneName.setFont(new Font (Font.DIALOG, Font.PLAIN, 18));
		add(g, c, sceneName, 1, 2, 2, 1);
	}
	public void addPackagePath() {
		JLabel tableNameLabel = new JLabel("包名：");
		tableNameLabel.setFont(new Font (Font.DIALOG, Font.BOLD, 18));
		add(g, c, tableNameLabel, 0, 1, 1, 1);
		this.packagePath = new JTextField(28);
		this.packagePath.setFont(new Font (Font.DIALOG, Font.PLAIN, 18));
		add(g, c, packagePath, 1, 1, 2, 1);
	}
	
	public static void main(String args[]){
		new Config("代码生成配置信息");
	}
}
